﻿using System.ComponentModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Clancy.Common;
using Clancy.Interfaces;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Reactive.Subjects;
using System.Windows;

namespace Clancy.Ui.ViewModels
{
    public class VideoSourceSelectionViewModel : ReactiveObject, IDisposable
    {
        private readonly BehaviorSubject<IVideoSourceFilterInfo> m_SelectedVideoSourceFilterInfo = new BehaviorSubject<IVideoSourceFilterInfo>(null);
        private readonly IVideoSourceProvider m_VideoSourceProvider;
        private ObservableAsPropertyHelper<IVideoSourceFilterInfo> m_SelectedVideoSourceFilterInfoHelper;
        private IObservable<EventPattern<PropertyChangedEventArgs>> m_AdditionalConfiguragionObservable;
        private IDisposable m_AdditionalConfiguragionSubscription;

        public VideoSourceSelectionViewModel(IEnumerable<IVideoSourceFilterInfo> availableVideoInputs)
        {
            AvailableVideoInputs = availableVideoInputs;

            InitProperties();

            InitCommands();
        }

        public IEnumerable<IVideoSourceFilterInfo> AvailableVideoInputs { get; private set; }

        public ReactiveCommand<object> CmdCancel { get; set; }

        public ReactiveCommand<object> CmdOk { get; set; }

        public IVideoSourceFilterInfo SelectedItem
        {
            get 
            {
                if (m_SelectedVideoSourceFilterInfoHelper == null)
                    return null;
                return m_SelectedVideoSourceFilterInfoHelper.Value; 
            }
            set { m_SelectedVideoSourceFilterInfo.OnNext(value); }
        }

        public Visibility AdditionalConfigurationViewState
        {
            get
            {
                if (SelectedItem == null)
                    return Visibility.Hidden;

                if (Constants.FixedInputSourceNames.Contains(SelectedItem.Name))
                    return Visibility.Visible;

                return Visibility.Hidden;
            }
        }

        public string AdditionalConfiguration
        {
            get
            {
                if (SelectedItem == null)
                    return string.Empty;

                return SelectedItem.MonikerString;
            }
            set
            {
                SelectedItem.MonikerString = value;
                this.RaisePropertyChanged();
            }
        }

        public void Dispose()
        {
            m_SelectedVideoSourceFilterInfo.Dispose();
            m_SelectedVideoSourceFilterInfoHelper.Dispose();
        }

        private static void CloseWindows(Window window, bool dialogResult)
        {
            if (window == null)
                return;

            window.DialogResult = dialogResult;
            window.Close();
        }

        private void InitCommands()
        {
            CmdOk = ReactiveCommand.Create(m_AdditionalConfiguragionObservable.Select(_ => IsOkButtonEnabled()));
            CmdOk.Subscribe(parameter => CloseWindows(parameter as Window, true));

            CmdCancel = ReactiveCommand.Create();
            CmdCancel.Subscribe(parameter => CloseWindows(parameter as Window, false));
        }

        private bool IsOkButtonEnabled()
        {
            if (AdditionalConfigurationViewState == Visibility.Hidden)
                return true;

            string testString = AdditionalConfiguration;
            int idx = testString.IndexOf("//");

            if (idx > -1)
            {
                testString = AdditionalConfiguration.Substring(idx + 2);
            }
            return !string.IsNullOrEmpty(testString);
        }

        private void InitProperties()
        {
            m_SelectedVideoSourceFilterInfoHelper = new ObservableAsPropertyHelper<IVideoSourceFilterInfo>(m_SelectedVideoSourceFilterInfo,
                _ =>
                {
                    this.RaisePropertyChanged("SelectedItem");
                    this.RaisePropertyChanged("AdditionalConfigurationViewState");
                    this.RaisePropertyChanged("AdditionalConfiguration");

                    if (SelectedItem != null)
                    {
                        AdditionalConfiguration = SelectedItem.MonikerString;
                    }
                });

            m_AdditionalConfiguragionObservable = Observable.FromEventPattern<PropertyChangedEventArgs>(this, "PropertyChanged");
                
        }       
    }
}